<?php
namespace app\payclass\model;

use app\BaseModel;
use think\facade\Db;

class Record extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_payclass_active_record';



    protected $append = [

        'sign_num',

        'sign_time',

        'atv_info',

        'class_status'

    ];


    /**
     * @author chenniang
     * @DataTime: 2020-12-26 22:08
     * @功能说明:课程状态
     */
    public function getClassStatusAttr($value,$data){

        if(!empty($data['class_id'])){

            $class_model = new ClassList();

            return $class_model->where(['id'=>$data['class_id']])->value('status');
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-15 15:31
     * @功能说明:活动详情
     */
    public function getAtvInfoAttr($value,$data){

        if(!empty($data['atv_id'])){

            $atv_model = new Active();

            $info = $atv_model->where(['id'=>$data['atv_id']])->field('cover')->find();

            return $info;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 15:23
     * @功能说明:报名时间
     */
    public function getSignTimeAttr($value,$data){

        if(!empty($data['create_time'])){

            return date('Y-m-d H:i:s',$data['create_time']);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 14:37
     * @功能说明:报名数量
     */
    public function getSignNumAttr($value,$data){

        if(!empty($data['id'])){

            $num = Db::name('longbing_card_payclass_record_list')->where(['record_id'=>$data['id']])->count();

            return $num;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 14:41
     * @功能说明:状态
     */
    public function getStatusAttr($value,$data){

        if(!empty($data['id'])&&isset($data['num'])&&isset($data['over_time'])){

            $num = Db::name('longbing_card_payclass_record_list')->where(['record_id'=>$data['id']])->count();
            //进行中
            if($num<$data['num']){

                $status = 1;
            }
            //活动已结束
            if($data['over_time']<time()){

                $status = 2;
            }
            //活动成功
            if($num>=$data['num']){

                $status = 3;
            }

            $this->where(['id'=>$data['id']])->update(['status'=>$status]);

            return $status;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function recordList($dis,$page){

      $data = $this->alias('a')
              ->join('longbing_card_user b','a.user_id = b.id','left')
              ->join('longbing_card_payclass_list c','a.class_id = c.id','left')
              ->where($dis)
              ->field('a.*,b.nickName,b.avatarUrl,c.title as class_name')
              ->group('a.id')
              ->order('a.id desc')
              ->paginate($page)
              ->toArray();

      return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function recordAdd($data){

        $data['status']      = 1;

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function recordUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-15 15:40
     * @功能说明:点赞详情列表
     */
    public function recordUserList($record_id,$page=10){

        $dis = [

            'a.record_id' => $record_id
        ];

        $data = Db::name('longbing_card_payclass_record_list')->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->where($dis)
                ->field(['b.nickName','b.avatarUrl',"FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i:%s') as create_time"])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-16 13:52
     * @功能说明:课程列表
     */
    public function classList($user_id,$page){

        $dis[] = ['a.user_id','=',$user_id];

        $data = $this->alias('a')
            ->join('longbing_card_payclass_list b','a.class_id = b.id')
            ->where($dis)
            ->field('a.id,a.class_id,b.title,b.cover,b.status,a.num,a.price,a.init_price,a.over_time,a.staff_id')
            ->group('a.id')
            ->order('a.id as desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-21 16:09
     * @功能说明:我的活动的id
     */
    public function myRecordId($class_id,$user_id){

        $class_model = new ClassList();

        $dis = [

            'id' => $class_id
        ];

        $class = $class_model->classInfo($dis);
        //查询课程权限
        $look_auth = $class_model->lookAuth($class,$user_id);
        //如果已经有该课程的权限就直接返回0
        if($look_auth==1){

            return 0;
        }

        $dis = [

            'a.class_id' => $class_id,

            'a.user_id'  => $user_id,

            'b.status'   => 1
        ];
        //我的记录
        $info  = $this->alias('a')
                ->join('longbing_card_payclass_active b','a.atv_id = b.id')
                ->field('a.*')
                ->where($dis)
                ->group('a.id')
                ->select()
                ->toArray();

        if(empty($info)){

            return 0;

        }

        foreach ($info  as $value){
            //只有进行中 或者 已经完成的才会返回活动的id
            if($value['status']==1||$value['status']==3){

                return $value['id'];
            }

        }

        return 0;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-22 16:01
     * @功能说明:检测过期
     */
    public function check($uniacid){

        $dis[] = ['uniacid','=',$uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['over_time','<',time()];

        $this->where($dis)->update(['status'=>2]);

        return true;

    }














}